package com.ynsj.controller;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ynsj.domain.Notice;
import com.ynsj.domain.SysUser;
import com.ynsj.model.R;
import com.ynsj.service.NoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Arrays;

@RestController
@Api(tags = "公告管理")
@RequestMapping("/notices")
public class NoticeController {

    @Autowired
    private NoticeService noticeService;

    @GetMapping
    @ApiOperation("分页查询公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "current",value = "当前页"),
            @ApiImplicitParam(name = "size",value = "页大小"),
            @ApiImplicitParam(name = "title",value = "标题"),
            @ApiImplicitParam(name = "startTime",value = "开始时间"),
            @ApiImplicitParam(name = "endTime",value = "结束时间"),
            @ApiImplicitParam(name = "status",value = "公告状态")
    })
    @PreAuthorize("hasAuthority('notice_query')")
    public R<Page<Notice>> findNoticePage(@ApiIgnore Page<Notice> noticePage, String title, String startTime, String endTime, Integer status){
        return R.ok(noticeService.findByPage(noticePage,title,startTime,endTime,status));
    }

    @PostMapping("/delete")
    @ApiOperation("删除公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ids",value = "删除的公告id")
    })
    @PreAuthorize("hasAuthority('notice_delete')")
    public R delNotice(@RequestBody String[] ids){
        boolean isOk = noticeService.removeByIds(Arrays.asList(ids));
        if (isOk){
            return R.ok("删除成功");
        }
        return R.fail("删除失败");
    }

    @PostMapping("/updateStatus")
    @ApiOperation("禁用/启用公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "要更新的公告id"),
            @ApiImplicitParam(name = "status",value = "更新的状态")
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R updateStatus(Long id, Integer status){
        boolean isOk = noticeService.updateStatus(id,status);
        if (isOk){
            return R.ok("禁用/启用成功");
        }
        return R.fail("禁用/启用失败");
    }

    @PostMapping
    @ApiOperation("新增一个公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice",value = "notice的json")
    })
    @PreAuthorize("hasAuthority('notice_create')")
    public R addNotice(@RequestBody @Validated Notice notice){
        boolean isOk = noticeService.save(notice);
        if (isOk){
            return R.ok("新增公告成功");
        }
        return R.fail("新增公告失败");
    }

    @PatchMapping
    @ApiOperation("修改一个公告")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "notice",value = "notice的json")
    })
    @PreAuthorize("hasAuthority('notice_update')")
    public R updateNotice(@RequestBody @Validated Notice notice){
        boolean isOk = noticeService.updateById(notice);
        if (isOk){
            return R.ok("更新公告成功");
        }
        return R.fail("更新公告失败");
    }
}
